package com.bsoft.gol.hbs.service.impl;

import ctd.controller.exception.ControllerException;
import ctd.dictionary.DictionaryItem;
import ctd.dictionary.controller.DictionaryController;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.JSONUtils;
import ctd.util.annotation.RpcService;
import com.bsoft.gol.hbs.dao.SysBillingRuleDAO;
import hcn.base.Doctor;
import hcn.base.feerule.SysBillingRule;
import com.bsoft.gol.mpi.model.Card;
import com.bsoft.gol.mpi.model.Certificate;
import com.bsoft.gol.mpi.model.Person;
import hcn.service.feerule.ISysBillingRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.bean.consultation.cloudpay.vo.PrescriptionOrder;
import com.bsoft.gol.hbs.utils.CommonUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author xhf
 * @date 2019/6/18 20:25
 * @Description
 */
@SsdevService("sysBillingRuleService")
public class SysBillingRuleServiceImpl implements ISysBillingRuleService {
    private final Logger logger = LoggerFactory.getLogger(SysBillingRuleServiceImpl.class);

    @Autowired
    private SysBillingRuleDAO sysBillingRuleDAO;

    @Override
    @RpcService
    public void saveSysBillingRule(SysBillingRule sysBillingRule) {
        if(CommonUtils.isEmpty(sysBillingRule.getSysBillingRuleId())){
            SysBillingRule exist = null;
            if(null != sysBillingRule.getDrugType()){
                exist = sysBillingRuleDAO.getSysPayCofByDrugType(sysBillingRule.getOrgId(), sysBillingRule.getModuleId(), sysBillingRule.getDeliveryModeCode(), sysBillingRule.getDrugType());
            }else {
                exist = sysBillingRuleDAO.getSysPayCofByDeliveryModeCode(sysBillingRule.getOrgId(), sysBillingRule.getModuleId(), sysBillingRule.getDeliveryModeCode());
            }
            if(null != exist){
                sysBillingRule.setSysBillingRuleId(exist.getSysBillingRuleId());
                sysBillingRuleDAO.update(sysBillingRule);
            }else {
                sysBillingRule.setSysBillingRuleId(UUID.randomUUID().toString());
                sysBillingRule.setGmtCreate(new Date());
                sysBillingRule.setGmtModified(new Date());
                sysBillingRuleDAO.save(sysBillingRule);
            }
        }else{
            sysBillingRuleDAO.update(sysBillingRule);
        }
    }

    @Override
    @RpcService
    public void deleteSysBillingRule(SysBillingRule sysBillingRule) {
         sysBillingRuleDAO.remove(sysBillingRule.getSysBillingRuleId());
    }

    @Override
    @RpcService
    public SysBillingRule getSysBillingRuleByOrgId (String orgId,String moduleId,String deliveryModeCode){
        return sysBillingRuleDAO.getSysPayCofByDeliveryModeCode(orgId,moduleId,deliveryModeCode);
    }


    @Override
    @RpcService
    public List<SysBillingRule> getList(String orgId, String moduleId) {
        return sysBillingRuleDAO.findSysPayCofList(orgId,moduleId);
    }
    @RpcService
    public List<DictionaryItem> aaa(String dicCode) throws ControllerException {
        ctd.dictionary.Dictionary sourceDic = DictionaryController.instance().get(dicCode);

        List<DictionaryItem> slice = sourceDic.getSlice(null, 0, null);

        Card card = new Card();
        card.setSource("618a16bf4178d15f7089ea9d");
        card.setCardType("01");
        card.setCardNo("");
        card.setAddTime(new Date());
        card.setCertified(false);
        card.setPatientCode("");
        card.setPlatformMpi("");

        logger.info(JSONUtils.writeValueAsString(card));
        return slice;
    }

    @RpcService
    public Card bbb() throws ControllerException {
        Card card = new Card();
        card.setSource("618a16bf4178d15f7089ea9d");
        card.setCardType("01");
        card.setCardNo("");
        card.setAddTime(new Date());
        card.setCertified(false);
        card.setPatientCode("");
        card.setPlatformMpi("");
        return card;
    }

    @RpcService
    public Doctor testGetDoctorDic() throws ControllerException {
        Doctor doctor = new Doctor();
        doctor.setSex("1");
        doctor.setLevel("231");


        return doctor;
    }


    @RpcService
    public Person testGetPersonDic() throws ControllerException {
        Person person = new Person();
        Certificate certificate = new Certificate();
        certificate.setCertificateType("01");
        certificate.setCertificateNo("3545646546");
        certificate.setSource("01");
        person.setCertificate(certificate);
        person.setProvince("110000000000");
        logger.info(JSONUtils.toString(person));;
        return person;
    }

    @RpcService
    public PrescriptionOrder testGetPrescriptionOrderDic() throws ControllerException {
        PrescriptionOrder prescriptionOrder = new PrescriptionOrder();
        prescriptionOrder.setPayStatus("1");

        return prescriptionOrder;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(com.alibaba.druid.filter.config.ConfigTools.decrypt("Xb6KgoRpVYPq24Wr6nRx2vmOwWHdCmfsxV0OMJvU0zW6EreG6BMzGJhff0BlLcyTcD8uJBjTsdBGWkI8PJgNKw=="));
    }

}
